package member.service;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import member.dao.MemberDAOImpl;
import member.vo.MemberVO;
import member.vo.ZipCodeVO;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.multipart.MultipartFile;


public class MemberServiceImpl implements MemberService{
	private MemberDAOImpl dao;
	public String uploadDirectory;
	private JavaMailSender mailSender;
	public MemberServiceImpl(MemberDAOImpl dao,String uploadDirectory,JavaMailSender mailSender){
		this.dao = dao;
		this.uploadDirectory = uploadDirectory;
		this.mailSender = mailSender;
	}

	/*
	 * 로그인 메소드
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:28:22
	 * @param vo
	 * @return
	 * @throws SQLException
	 */
	@Override
	public MemberVO login(MemberVO vo) throws SQLException {
		return dao.login(vo);
	}

	/*
	 * 우편 번호 찾기
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:35:52
	 * @param dong
	 * @return
	 * @throws SQLException
	 */
	@Override
	public ArrayList<ZipCodeVO> zipCodeSelect(String dong) throws SQLException {
		return dao.zipCodeSelect(dong);
	}

	/*
	 * 아이디 중복 체크 없어도 되는 메소드 예시로 남겨 놓은것
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:36:02
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	@Override
	public String checkId(String id) throws SQLException {
		String member_id = dao.checkId(id);
		String message = "";
		if(member_id!=null){
			message = id + " 는 이미 사용중인 아이디 입니다.";
		}else{
			message = id + " 는 사용 가능한 아아디 입니다.";
		}
		return message;
	}

	/*
	 * 회원가입
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:36:22
	 * @param vo
	 * @return
	 * @throws SQLException
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@Override
	public MemberVO memberJoin(MemberVO vo) throws SQLException, IllegalStateException, IOException {
		if(!vo.getUpfile().isEmpty()){
			String originalFilename = (String)vo.getUpfile().getOriginalFilename();
			String type = originalFilename.substring((originalFilename.lastIndexOf(".")+1));
			long time = System.currentTimeMillis();
			vo.getUpfile().transferTo(new File(uploadDirectory,"mem"+time+"."+type));
			vo.setPicture_name("mem"+time+"."+type);
		}
		dao.memberJoin(vo);
		return dao.login(vo);
	}

	/*
	 * 아이디 찾기
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:36:33
	 * @param vo
	 * @return
	 * @throws SQLException
	 */
	@Override
	public MemberVO serchId(MemberVO vo) throws SQLException {
		return dao.serchId(vo);
	}

	/*
	 * 메일 발송
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:36:41
	 * @param vo
	 * @return
	 * @throws SQLException
	 * @throws MessagingException
	 */
	@Override
	public String sendMail(MemberVO vo) throws SQLException, MessagingException {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper messageHelper;
		MemberVO mvo = dao.sendMail(vo);
		String email = mvo.getEmail()+"@"+mvo.getEmail_type();
		messageHelper = new MimeMessageHelper(message,true,"UTF-8");
		messageHelper.setSubject("찾으시는 아이디입니다.");
		messageHelper.setTo(email);
		messageHelper.setCc("bum_young@nate.com");
		messageHelper.setBcc("bum_young@nate.com");
		messageHelper.setFrom("gpskost@gmail.com");
		messageHelper.setText(mvo.getName()+" 님이 찾는 아이디는"+mvo.getMember_id()+" 입니다.",true);
		mailSender.send(message);
		return null;
	}

	/*
	 * 패스워드 변경
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:36:47
	 * @param vo
	 * @return
	 * @throws SQLException
	 */
	@Override
	public int changePassword(MemberVO vo) throws SQLException {
		return dao.changePassword(vo);
	}

	/*
	 * 패스워드 검색
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:36:59
	 * @param vo
	 * @return
	 * @throws SQLException
	 */
	@Override
	public MemberVO searchPass(MemberVO vo) throws SQLException {
		return dao.searchPass(vo);
	}

	/*
	 * 개인정보 수정
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:37:08
	 * @param vo
	 * @return
	 * @throws SQLException
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@Override
	public int memberModify(MemberVO vo) throws SQLException,IllegalStateException, IOException {
		MultipartFile upfile = vo.getUpfile();
		if(upfile != null && !upfile.isEmpty()){
			String originalFilename = (String)vo.getUpfile().getOriginalFilename();
			String type = originalFilename.substring((originalFilename.lastIndexOf(".")+1));
			long time = System.currentTimeMillis();
			vo.getUpfile().transferTo(new File(uploadDirectory,"mem"+time+"."+type));
			vo.setPicture_name("mem"+time+"."+type);
		}
		return dao.memberModify(vo);
	}
}













